var UILoading, ns, ajpush, count = 0,
    toast;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            Username: '',
            Password: '',
            action: '',
            device_type: '',
            SystemNumber: ''
        },
        device_id: '',
        isSingleSite: CONFIG.isSingleSite
    },
    methods: {
        get_api: function() {
            var that = this;
            if (that.isSingleSite) {
                api.setPrefs({
                    key: 'apiurl',
                    value: CONFIG.apiurl
                });
                that.do_login();
                return;
            }
            if (that.form.SystemNumber == '') {
                api.toast({
                    msg: '请输入系统编号',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.Username == '') {
                api.toast({
                    msg: '请输入用户名',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.Password == '') {
                api.toast({
                    msg: '请输入密码',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            toast.loading({
                title: "加载中",
                duration: 2000
            }, function(ret) {});
            that.form.action = "getapi";
            ns.post(that.form, function(succeed, data, err) {
                if (succeed) {
                    api.setPrefs({
                        key: 'apiurl',
                        value: data.apiurl
                    });
                    api.setPrefs({
                        key: 'companyname',
                        value: data.companyname
                    });
                    setTimeout(function() {
                        that.do_login();
                    }, 200)
                } else if (err) {
                    toast.hide();
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                is_getapi: true
            });
        },
        do_login: function() {
            var that = this;
            if (!that.form.Username) {
                toast.hide();
                api.toast({
                    msg: '请输入用户名',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.Password) {
                toast.hide();
                api.toast({
                    msg: '请输入密码',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.device_id == '' && count <= 1) {
                that.get_jiguang_register_id();
                count++;
                return;
            }
            if (that.device_id == '') {
                api.toast({
                    msg: '极光id获取失败',
                    duration: 1000,
                    location: 'bottom'
                });
            }
            that.form.action = "login";
            ns.post(that.form, function(succeed, data, err) {
                toast.hide();
                if (succeed) {
                    if (data.uid) {
                        api.setPrefs({
                            key: 'uid',
                            value: data.uid
                        });
                    }
                    if (that.form.Username) {
                        api.setPrefs({
                            key: 'loginname',
                            value: that.form.Username
                        });
                    }
                    if (data.username) {
                        api.setPrefs({
                            key: 'username',
                            value: data.username
                        });
                    }
                    if (data.headimg) {
                        api.setPrefs({
                            key: 'headimg',
                            value: data.headimg
                        });
                    }
                    if (data.phonenumber) {
                        api.setPrefs({
                            key: 'phonenumber',
                            value: data.phonenumber
                        });
                    }
                    if (data.positionname) {
                        api.setPrefs({
                            key: 'positionname',
                            value: data.positionname
                        });
                    }
                    if (data.companyname) {
                        api.setPrefs({
                            key: 'companyname',
                            value: data.companyname
                        });
                    }
                    api.sendEvent({
                        name: 'onlogin'
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                is_login: true
            });
        },
        get_jiguang_register_id: function() {
            var that = this;
            ajpush.getRegistrationId(function(res, err) {
                if (res && res.id) {
                    that.device_id = res.id;
                    api.setPrefs({
                        key: 'jpush_device_id',
                        value: res.id
                    });
                }
            });
        }
    }
});

apiready = function() {
    UILoading = api.require('UILoading');
    ns = window.Foresight.Util;
    ajpush = api.require('ajpush');
    toast = new auiToast();
    app.get_jiguang_register_id();
    app.form.device_type = api.systemType;
}
